package com.ftg.learn.ai.config;

import com.ftg.learn.ai.properties.ModelProperties;
import com.ftg.learn.ai.servcie.LegalAssistant;
import com.ftg.learn.ai.servcie.ReplyAssistant;
import com.ftg.learn.ai.servcie.StartAssistant;
import dev.langchain4j.memory.chat.MessageWindowChatMemory;
import dev.langchain4j.model.chat.ChatModel;
import dev.langchain4j.model.chat.StreamingChatModel;
import dev.langchain4j.model.openai.OpenAiChatModel;
import dev.langchain4j.model.openai.OpenAiStreamingChatModel;
import dev.langchain4j.service.AiServices;
import dev.langchain4j.service.MemoryId;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Slf4j
@Configuration
public class AiConfig {
    @Bean
    public StreamingChatModel streamingChatLanguageModel(ModelProperties modelProperties){
        log.info("开始创建模型对象：{}", modelProperties);
        return OpenAiStreamingChatModel.builder()
                .apiKey(modelProperties.getApiKey())
                .modelName(modelProperties.getModelName())
                .baseUrl(modelProperties.getBaseUrl())
                .build();
    }
    @Bean
    public LegalAssistant legalAssistant(StreamingChatModel streamingChatLanguageModel) {
        return AiServices.builder(LegalAssistant.class)
                .streamingChatModel(streamingChatLanguageModel)
                .chatMemoryProvider(MemoryId-> MessageWindowChatMemory.withMaxMessages(20))
                .build();
    }
    @Bean
    public ChatModel chatLanguageModel(ModelProperties modelProperties){
        log.info("开始创建模型对象：{}", modelProperties);
        return OpenAiChatModel.builder()
                .apiKey(modelProperties.getApiKey())
                .modelName(modelProperties.getModelName())
                .baseUrl(modelProperties.getBaseUrl())
                .build();
    }
    @Bean
    public StartAssistant startAssistant(ChatModel chatLanguageModel) {
        return AiServices.create(StartAssistant.class, chatLanguageModel);
    }

    @Bean
    public ChatModel ReplyAssistant(ModelProperties modelProperties){
        log.info("开始创建模型对象：{}", modelProperties);
        return OpenAiChatModel.builder()
                .apiKey(modelProperties.getApiKey())
                .modelName(modelProperties.getModelName())
                .baseUrl(modelProperties.getBaseUrl())
                .build();
    }
    @Bean
    public ReplyAssistant replyAssistant(ChatModel chatLanguageModel) {
        return AiServices.create(ReplyAssistant.class, chatLanguageModel);
    }
}
